package com.wy.powerbuilder.controller.ljh;

import com.github.pagehelper.PageInfo;
import com.wy.powerbuilder.entity.ljh.Device;
import com.wy.powerbuilder.entity.ljh.Type;
import com.wy.powerbuilder.entity.lwg.houses;
import com.wy.powerbuilder.service.ljh.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class DeviceController {
    @Autowired
    private DeviceService deviceService;
    @RequestMapping(value = {"/selDevice"})
    public  String selByPage(Integer pageNum, Integer pageSize, Model models,String model){
        pageSize=2
;
        PageInfo<Device> pageInfoes= deviceService.selByPage(pageNum,pageSize,model);
        models.addAttribute("pageInfoes",pageInfoes);
        if(model==null){
            model="";
        }
        models.addAttribute("model",model);
        return "device-list";
    }
    @RequestMapping(value = {"/showDevice"})
    public String seleAll(Model model){
        List<houses> houes=deviceService.seleAll();
        List<Type> types=deviceService.all();
        model.addAttribute("houes",houes);
        model.addAttribute("types",types);
        return "device-add";
    }
    @RequestMapping(value = {"/delDevice"})
    public String delDevice(Integer deviceid){

        int row=deviceService.delDevice(deviceid);
        if (row>0){
            return "redirect:/selDevice";
        }else {
            return "";
        }
    }
    @ResponseBody
    @RequestMapping("addDevice")
    public String addDevice(Device device){
        System.out.println(device.getH_buliding());
        int i=deviceService.addDevice(device);
        if(i>0){
            return "ok";
        }else{
            return "no";
        }
    }
        @RequestMapping(value = {"/seldeviceId"})
    public String seldeviceId(Integer deviceid,Model model){
        Device device=deviceService.seldeviceId(deviceid);
            List<houses> houes=deviceService.seleAll();
            List<Type> types=deviceService.all();
            model.addAttribute("houes",houes);
            model.addAttribute("types",types);
            model.addAttribute("device",device);
        return "device-update";
        }
    @RequestMapping(value = {"/updDevice"})
    public String updDevice(Device device){
       int row=deviceService.updDevice(device);
        if(row>0){
            return "redirect:/selDevice";
        }else{
            return "";
        }
    }
}
